package com.huawei.petapp2.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.GoodDatailActivity;
import com.huawei.petapp2.entity.Storeitem;

import java.util.List;

public class SearchGoodsAdapter extends RecyclerView.Adapter<SearchGoodsAdapter.myViewHolder> {
    private List<Storeitem>storeitems;
    private Context context;
    private View inflater;
    MyItemClickListener mItemClickListener;
    Bundle bundle;

    public SearchGoodsAdapter(Context context, List<Storeitem>storeitems, Bundle bundle)
    {
        this.context=context;
        this.storeitems=storeitems;
        this.bundle=bundle;
    }

    public void setOnItemClickListener(MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface MyItemClickListener {
        public void onItemClick(View view, int postion);
    }

    @NonNull
    @Override
    public SearchGoodsAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.good_result_item_activity,parent,false);
        myViewHolder myViewHolder = new myViewHolder(inflater);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SearchGoodsAdapter.myViewHolder holder, int position) {
        Storeitem storeitem = storeitems.get(position);
        Glide.with(context)
                .load(storeitem.getItemImg())

                .into(holder.goodImage);
        holder.goodPrice.setText("￥"+storeitem.getItemPrice());
        holder.goodName.setText(storeitem.getItemName());
        holder.goodDes.setText(storeitem.getItemDes());
        holder.goodImage.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view1) {
                // 获取点击的位置，定位数组并输出信息
                int adapterPosition = holder.getAdapterPosition();

                Toast.makeText(view1.getContext(), "点击的布局，其中内容为: " + storeitem.getItemName(), Toast.LENGTH_SHORT).show();
                Storeitem storeitemClick=storeitem;
                Intent intent=new Intent(context, GoodDatailActivity.class);
                intent.putExtra("clickGood",  storeitemClick);//点击商品项
                System.out.println("在传输方法中打印"+storeitemClick);
                // Log.d("JunLog",mGoodsList.toArray()+"");
                intent.setClass(context,GoodDatailActivity.class);
                context.startActivity(intent,bundle);
            }
        });
    }


    @Override
    public int getItemCount() {
        return storeitems.size();
    }

    class myViewHolder extends RecyclerView.ViewHolder{
        View goodView;
        ImageView goodImage;
        TextView goodName;
        TextView goodDes;
        TextView goodPrice;

        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            goodView = itemView;
            goodImage = itemView.findViewById(R.id.item_good_img);
            goodPrice = itemView.findViewById(R.id.item_good_price);
            goodName= itemView.findViewById(R.id.item_good_name);
            goodDes= itemView.findViewById(R.id.item_good_desc);
        }
    }
}
